﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nonocast.Scheduling {
	public static class FluentTaskRule {
		public static TaskRule From(this TaskRule self, DateTime time) {
			self.StartTime = time;
			return self;
		}

		public static TaskRule From(this TaskRule self, string time) {
			return self.From(DateTime.Parse(time));
		}

		public static TaskRule Until(this TaskRule self, DateTime time) {
			self.ExpirationTime = time;
			return self;
		}

		public static TaskRule Until(this TaskRule self, string time) {
			return self.Until(DateTime.Parse(time));
		}

		public static TaskRule FromDayBeginning(this TaskRule self) {
			self.StartTime = DateTime.Parse("0:00");
			return self;
		}

		public static TaskRule UntilDayEnd(this TaskRule self) {
			self.ExpirationTime = null;
			return self;
		}

		public static TaskRule AllDay(this TaskRule self) {
			return self.FromDayBeginning().UntilDayEnd();
		}

		// For Once
		public static TaskRule Today(this TaskRule self) {
			(self as Once).SpecificDate = DateTime.Now.Date;
			return self;
		}

		public static TaskRule In(this TaskRule self, string date) {
			(self as Once).SpecificDate = DateTime.Parse(date).Date;
			return self;
		}

		// For DaysOfWeek
		public static TaskRule EveryDay(this TaskRule self) {
			var rule = self as DaysOfWeek;
			rule.WeekMask = new WeekMask(Days.All);
			return self;
		}

		public static TaskRule EveryWorkday(this TaskRule self) {
			var rule = self as DaysOfWeek;
			rule.WeekMask = new WeekMask(Days.Monday, Days.Tuesday, Days.Wednesday, Days.Thursday, Days.Friday);
			return self;
		}

		public static TaskRule Weekends(this TaskRule self) {
			var rule = self as DaysOfWeek;
			rule.WeekMask = new WeekMask(Days.Saturday, Days.Sunday);
			return self;
		}

		public static TaskRule Monday(this TaskRule self) {
			return Append(self, Days.Monday);
		}

		public static TaskRule Tuesday(this TaskRule self) {
			return Append(self, Days.Tuesday);
		}

		public static TaskRule Wednesday(this TaskRule self) {
			return Append(self, Days.Wednesday);
		}

		public static TaskRule Thursday(this TaskRule self) {
			return Append(self, Days.Thursday);
		}

		public static TaskRule Friday(this TaskRule self) {
			return Append(self, Days.Friday);
		}

		public static TaskRule Saturday(this TaskRule self) {
			return Append(self, Days.Saturday);
		}

		public static TaskRule Sunday(this TaskRule self) {
			return Append(self, Days.Sunday);
		}

		public static TaskRule Mon(this TaskRule self) {
			return Append(self, Days.Monday);
		}

		public static TaskRule Tue(this TaskRule self) {
			return Append(self, Days.Tuesday);
		}

		public static TaskRule Wed(this TaskRule self) {
			return Append(self, Days.Wednesday);
		}

		public static TaskRule Thu(this TaskRule self) {
			return Append(self, Days.Thursday);
		}

		public static TaskRule Fri(this TaskRule self) {
			return Append(self, Days.Friday);
		}

		public static TaskRule Sat(this TaskRule self) {
			return Append(self, Days.Saturday);
		}

		public static TaskRule Sun(this TaskRule self) {
			return Append(self, Days.Sunday);
		}

		private static TaskRule Append(TaskRule self, Days day) {
			var rule = self as DaysOfWeek;
			if (rule.WeekMask != null) {
				rule.WeekMask.Append(day);
			} else {
				rule.WeekMask = new WeekMask(day);
			}
			return rule;
		}
	}

}
